package com.jxj.GUI;

import com.jxj.mapper.mapperUser;
import com.jxj.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

class add extends JFrame {
    private SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
    mapperUser mapperuser = sqlSession.getMapper(mapperUser.class);
    private List<JTextField> textFieldList = new ArrayList<>();

    public void addWindow() {
        JFrame frame = new JFrame("添加学生信息");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        int DIALOG_WHITE = 600;
        int DIALOG_HEIGHT = 600;
        Point point = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        frame.setBounds(point.x - DIALOG_WHITE / 2, point.y - DIALOG_HEIGHT / 2, DIALOG_WHITE, DIALOG_HEIGHT);
        frame.setVisible(true);
        frame.setIconImage(new ImageIcon("C:\\Users\\Lenovo\\Desktop\\icon.png").getImage());
        String[] list = {"学生编号", "学生姓名", "年龄", "专业", "性别"};
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(15, 15, 15, 15); // 设置组件之间的间距
        // 插入六行
        for (int i = 0; i < 5; i++) {
            JLabel label = new JLabel(list[i]);
            JTextField textField = new JTextField(20);
            textFieldList.add(textField);
            textField.setBorder(null);
            textField.setPreferredSize(new Dimension(200, 30)); // 设置文本框大小为200x30
            textField.setMargin(new Insets(5, 5, 5, 5));
            // 添加标签和文本框到主容器中
            gbc.gridy = i;
            mainPanel.add(label, gbc);
            gbc.gridx = 1;
            mainPanel.add(textField, gbc);
            // 重置横向位置
            gbc.gridx = 0;
        }
        JButton confirmButton = new JButton("确定");
        confirmButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    int count = addstu();
                    if (count > 0) {
                        frame.setVisible(false);
                        JOptionPane.showMessageDialog(
                                frame, // 父组件，如果为 null，则弹窗会显示在屏幕中央
                                "添加成功,数据已更新", // 提示消息
                                "提示", // 标题
                                JOptionPane.INFORMATION_MESSAGE // 提示类型，这里使用信息类型
                        );
                    } else {
                        frame.setVisible(false);
                        JOptionPane.showMessageDialog(
                                frame, // 父组件，如果为 null，则弹窗会显示在屏幕中央
                                "添加失败", // 提示消息
                                "提示", // 标题
                                JOptionPane.INFORMATION_MESSAGE // 提示类型，这里使用信息类型
                        );
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }

            }
        });

        changeButton(confirmButton);

        gbc.gridx = 1;
        gbc.gridy = 5; // 最后一行
        gbc.anchor = GridBagConstraints.LAST_LINE_END; // 设置对齐方式为最后一行的右下角
        mainPanel.add(confirmButton, gbc);
        // 将主容器添加到窗口中
        frame.add(mainPanel);
    }

    private int addstu() throws SQLException {
        int a = Integer.parseInt(textFieldList.get(0).getText());
        String b = textFieldList.get(1).getText();
        int c = Integer.parseInt(textFieldList.get(2).getText());
        String d = textFieldList.get(3).getText();
        String e = textFieldList.get(4).getText();
        int c1 = mapperuser.addI1(a, b, c, d, e);
        int aa = Integer.parseInt(textFieldList.get(0).getText());
        int c2 = mapperuser.addI2(aa);
        int count = c1 + c2;
        return count;
    }

    public void changeButton(JButton Button) {
        Button.setBorderPainted(false); // 不绘制按钮边框
        Button.setFocusPainted(false);
        Button.setRolloverEnabled(false);
        Button.setPressedIcon(null);
        Button.setForeground(Color.WHITE); // 设置文字颜色为白色
        Button.setBackground(new Color(0, 90, 141, 255)); // 设置背景色
    }
}
